<?php

namespace app\admin\model;

use think\Model;
use think\Db;
use think\Exception;//事务处理

class Position extends Model
{
    //职务表
    public function getposition($res)
    {
        $where['a.lang'] = $res['lang'];//语言标签
        $where['b.country_id'] = $res['country_id'];//国家标签
        $join = [
            ['nt_position b', 'a.mkey=b.mkey']
        ];
        $table = 'multi_lang';
        if (isset($res['dname']) && $res['dname']) {
            $where['a.content'] = array('like', '%' . removeBlank($res['dname']) . '%');//职位名称搜索
            $resdata = getmultidata($table, $join, $where);
            $data = $resdata['data'];//数据
            $count = $resdata['count'];//总数
            foreach ($data as $datakey => $dataval) {
                $data[$datakey]['level'] = 0;
                //职位下的员工的数量
                $data[$datakey]['num'] = Db::name('admin')->where(['position_id' => $dataval['id']])->count();
            }
        } else {
            $resdata = getmultidata($table, $join, $where);
            $data = getTree($resdata['data']);
            $count = $resdata['count'];
            foreach ($data as $datakey => $dataval) {
                //职位下的员工的数量
                $data[$datakey]['num'] = Db::name('admin')->where(['position_id' => $dataval['id']])->count();
            }
        }
        return array('count' => $count, 'data' => $data);

    }

    //删除该职位
    public function delposition($data)
    {
        //设置管理员无法删除
        if ($data['id'] == 1) {
            return array('msg' => lang("global_administrator_can't_delete"), 'status' => 0);
            exit;
        }
        $lang = $data['lang'];
        //查询具体语言下的职位
        $positionwhere['mkey'] = 'position_' . $data['id'];
        $langname = getlangname('multi_lang', $positionwhere, $lang);
        //职务下含有用户，不允许删除
        $info = Db::name('admin')->where('position_id', $data['id'])->find();
        if ($info) {
            return array('msg' => $langname . lang("contains_employees_not_allowed_to_delete"), 'status' => 0);
        }
        //职位下含有子职位不允许删除
        $inforole = $this->where('pid', $data['id'])->find();
        if ($inforole) {
            return array('msg' => $langname . lang("contains_sub-post_not_allowed_to_delete"), 'status' => 0);
        }
        Db::startTrans();
        $retrole = $this->where('id', $data['id'])->delete();//职位表
        $langdel = Db::name('multi_lang')->where('mkey', 'position_' . $data['id'])->delete();
        if ($retrole && $langdel) {
            Db::commit();
            return array('msg' => $langname . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $langname . lang("failed_to_delete"), 'status' => 0);
        }
    }

    //职位的添加与编辑
    public function addeditposition($data)
    {
        if ($data['act'] == 'positionadd' || $data['act'] == 'positionadds' || $data['act'] == 'positioncopy') {
            //添加，子添加，复制
            $ckdata = notEmpty(['position' => lang("post_name")]);
        } else {
            //编辑
            $dname_zh = trim($data['lang_zh']);//中文
            $dname_en = trim($data['lang_en']);//英文
            $dname_pt = trim($data['lang_pt']);//葡萄牙文
            $ckdata = notEmpty(['lang_zh' => lang("Chinese_job_name"), 'lang_en' => lang("English_job_name"), 'lang_pt' => lang("Poruguese_job_name")]);
        }
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        $lang = $data['lang'];
        unset($data['lang']);
        //添加职位 添加子职位 复制职位
        if ($data['act'] == 'positionadd' || $data['act'] == 'positionadds' || $data['act'] == 'positioncopy') {
            //判断职位名称是否存在
            $depalike = 'position';
            $langwhere['mkey'] = array('like', $depalike . '%');
            $positioname = $langwhere['content'] = trim($data['position']);
            $langzh = getables('multi_lang', $langwhere);
            if ($langzh) {
                return array('msg' => $positioname . lang("job_name_exists"), 'status' => 3);
            }
            Db::startTrans();
            $data['create_at'] = $data['update_at'] = time();
            //复制时需要获取这个职位的权限
            if ($data['act'] == 'positioncopy') {
                $data['auth_ids'] = $this->where('id', $data['id'])->value('auth_ids');//权限id
            }
            unset($data['id']);
            $positionid = $this->strict(false)->insertGetId($data);//职务表
            //更新数据
            $positionidmkey = 'position_' . $positionid;
            $this->where('id', $positionid)->setField('mkey', $positionidmkey);
            $langdata = getables('language');
            $multidata = array();
            foreach ($langdata as $langdatakey => $langdataval) {
                $multidata[$langdatakey]['content'] = $positioname;
                $multidata[$langdatakey]['mkey'] = $positionidmkey;
                $multidata[$langdatakey]['lang'] = $langdataval['code'];
            }
            $langid = Db::name('multi_lang')->insertAll($multidata);//语言表
            if ($positionid && $langid) {
                Db::commit();
                return array('msg' => $positioname . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $positioname . lang("add_failed"), 'status' => 0);
            }
        } elseif ($data['act'] == 'positionedit') {
            switch ($lang) {
                case 'zh':
                    $langedit = $dname_zh;
                    break;
                case 'en':
                    $langedit = $dname_en;
                    break;
                case 'pt':
                    $langedit = $dname_pt;
                    break;
                default:
                    $langedit = $dname_zh;
                    break;
            }
            if ($data['id'] == $data['pid']) {
                return array('msg' => $langedit . lang("edited_wrong"), 'status' => 0);
            }
            $inpid = getinfo($data['pid'], 'position')['pid'];//要编辑部门的pid
            if ($inpid == $data['id']) {
                return array('msg' => $langedit . lang("edited_wrong"), 'status' => 0);
            }
            Db::startTrans();
            $datarole['update_at'] = time();
            $datarole['pid'] = $data['pid'];
            $datarole['country_id'] = $data['country_id'];
            $resupid = $this->where('id', $data['id'])->update($datarole);//职务表

            //更新语言表
            $positionkey = 'position_' . $data['id'];
            $langupidzh = Db::name('multi_lang')->where(['mkey' => $positionkey, 'lang' => 'zh'])->setField('content', $dname_zh);//更新中文部门名称
            $langupiden = Db::name('multi_lang')->where(['mkey' => $positionkey, 'lang' => 'en'])->setField('content', $dname_en);//更新英文部门名称
            $langupidpt = Db::name('multi_lang')->where(['mkey' => $positionkey, 'lang' => 'pt'])->setField('content', $dname_pt);//更新葡文部门名称
            if ($resupid !== false && $langupidzh !== false && $langupiden !== false && $langupidpt !== false) {
                Db::commit();
                return array('msg' => $langedit . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $langedit . lang("edit_failed"), 'status' => 0);
            }
        }
    }

    //职位设置权限
    public function positionroot($data)
    {
        if (!isset($data['auth_ids'])) {
            return array('msg' => lang("the_permission_can't_be_empty"), 'status' => 2);
        }
        $lang = $data['lang'];
        //查询具体语言下的职位
        $positionwhere['mkey'] = 'position_' . $data['id'];
        $langname = getlangname('multi_lang', $positionwhere, $lang);

        $auth_ids = $data['auth_ids'];
        //通过里面的id，判断最顶级的id是否写入
        $menupid = Db::name('menu')->where(['pid' => 0, 'disable' => 0])->select();
        foreach ($menupid as $menupidkey => $menupidval) {
            //获取顶级下的二级
            $idarr = array_column(Db::name('menu')->where(['pid' => $menupidval['id'], 'disable' => 0])->select(), 'id');
            $arrempty=array_intersect($idarr, $auth_ids);
            if(!empty($arrempty)){
                array_push($auth_ids,$menupidval['id']);
            }
        }
        $update['auth_ids'] = implode(",", $auth_ids);
        $update['update_at'] = time();
        $upids = $this->where('id', $data['id'])->update($update);
        if ($upids !== false) {
            return array('msg' => $langname . lang("setting_permission_successfully"), 'status' => 1);
        } else {
            return array('msg' => $langname . lang("failed_to_setting_permission"), 'status' => 0);
        }
    }
}